package com.simple.create.builder;

import java.util.Date;

/**
 * 建造者模式
 * <p>
 * 为一个类提供链式调用注入成员变量值并构建实例的模式
 * <p>
 * 新建类Coupon的内部静态类CouponBuilder作为建造者
 * 建造者首先要知道自己要建造的是啥(Coupon类的实例)
 * 所以, CouponBuilder类应该拥有和Coupon类一样的成员变量
 * 然后写建造者建造的过程, 即向实例中注入成员变量的方法
 * 由于建造的成员变量一般比较多, 可以考虑在调用的时候采用链式风格
 * 所以在每个注入成员变量的方法注入完成后, 可以返回当前的这个建造者实例
 * 以便供下一个成员变量的注入
 * 在所有成员变量注入完成后, 即建造完成后
 * 需要返回Coupon类的实例
 * 所以提供build方法, 传入建造者, 调用Coupon类的构造方法
 * 在Coupon类的构造方法中,
 * 需要将建造者的成员变量赋值给Coupon类的成员变量
 * 这样就完成了一个Coupon类的实例的创建
 * <p>
 * 调用方使用:
 * 调用者需要Coupon类的实例,
 * 采用Coupon类的建造者进行创建
 * 可以在Coupon类中提供静态方法, 返回一个新的建造者
 * 然后正常调用链式方法进行赋值
 * 最后调用建造者的build方法
 * 完成了一个Coupon类的实例的创建
 *
 * @author simple
 * @version 1.0
 * @date 2018-12-10 10:19
 * @since 1.0
 */
public class Coupon {
    private Integer id;
    private String code;
    private String name;
    private Double oriPrice;
    private Double price;
    private Integer num;
    private Date startTime;
    private Date endTime;
    private String desc;
    private String remark;
    private Date createDate;
    private String createBy;
    private Date updateDate;
    private String updateBy;
    private Integer isDel;

    private Coupon(CouponBuilder builder) {
        this.id = builder.id;
        this.code = builder.code;
        this.name = builder.name;
        this.oriPrice = builder.oriPrice;
        this.price = builder.price;
        this.num = builder.num;
        this.startTime = builder.startTime;
        this.endTime = builder.endTime;
        this.desc = builder.desc;
        this.remark = builder.remark;
        this.createDate = builder.createDate;
        this.createBy = builder.createBy;
        this.updateDate = builder.updateDate;
        this.updateBy = builder.updateBy;
        this.isDel = builder.isDel;
    }

    public static CouponBuilder builder() {
        return new CouponBuilder();
    }

    @Override
    public String toString() {
        return "Coupon{" +
                "id=" + id +
                ", code='" + code + '\'' +
                ", name='" + name + '\'' +
                ", oriPrice=" + oriPrice +
                ", price=" + price +
                ", num=" + num +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", desc='" + desc + '\'' +
                ", remark='" + remark + '\'' +
                ", createDate=" + createDate +
                ", createBy='" + createBy + '\'' +
                ", updateDate=" + updateDate +
                ", updateBy='" + updateBy + '\'' +
                ", isDel=" + isDel +
                '}';
    }

    public static class CouponBuilder {
        private Integer id;
        private String code;
        private String name;
        private Double oriPrice;
        private Double price;
        private Integer num;
        private Date startTime;
        private Date endTime;
        private String desc;
        private String remark;
        private Date createDate;
        private String createBy;
        private Date updateDate;
        private String updateBy;
        private Integer isDel;

        public CouponBuilder id(Integer id) {
            this.id = id;
            return this;
        }

        public CouponBuilder code(String code) {
            this.code = code;
            return this;
        }

        public CouponBuilder name(String name) {
            this.name = name;
            return this;
        }

        public CouponBuilder oriPrice(Double oriPrice) {
            this.oriPrice = oriPrice;
            return this;
        }

        public CouponBuilder price(Double price) {
            this.price = price;
            return this;
        }

        public CouponBuilder num(Integer num) {
            this.num = num;
            return this;
        }

        public CouponBuilder startTime(Date startTime) {
            this.startTime = startTime;
            return this;
        }

        public CouponBuilder endTime(Date endTime) {
            this.endTime = endTime;
            return this;
        }

        public CouponBuilder desc(String desc) {
            this.desc = desc;
            return this;
        }

        public CouponBuilder remark(String remark) {
            this.remark = remark;
            return this;
        }

        public CouponBuilder createDate(Date createDate) {
            this.createDate = createDate;
            return this;
        }

        public CouponBuilder createBy(String createBy) {
            this.createBy = createBy;
            return this;
        }

        public CouponBuilder updateDate(Date updateDate) {
            this.updateDate = updateDate;
            return this;
        }

        public CouponBuilder updateBy(String updateBy) {
            this.updateBy = updateBy;
            return this;
        }

        public CouponBuilder isDel(Integer isDel) {
            this.isDel = isDel;
            return this;
        }

        public Coupon build() {
            return new Coupon(this);
        }
    }
}
